package http_routers

import (
	"PackingScanApp/com_operation"
	"PackingScanApp/db_mysql_server"
	"PackingScanApp/models"
	"PackingScanApp/view_body"

	"PackingScanApp/util"
	"encoding/json"
	"errors"

	"fmt"
	"net/http"
	"strconv"
)

//getNowProduction
func handleFunc(w http.ResponseWriter, r *http.Request) {
	var errmsg models.ErrMsg
	defer func() {
		if err := recover(); err != nil {
			errmsg.Errcode = 1
			errmsg.Errmsg = fmt.Sprintf("%s", err)
			if emsg, err := json.Marshal(errmsg); err == nil {
				w.Write([]byte(string(emsg)))
			}
		}
	}() //--

	w.Header().Set("content-type", "application/json")
	r.ParseForm()
	if len(r.Form["dofunc"]) > 0 {
		if r.Form["dofunc"][0] == "doNowPackingData" {
			if len(r.Form["productsType"]) > 0 && len(r.Form["productsName"]) > 0 && len(r.Form["inputUserId"]) > 0 && len(r.Form["password"]) > 0 {

				var packagingUser models.PackagingUser

				var passwordmd5 string = util.GetMD5(r.Form["password"][0])
				if err_s := db_server_mysql.SelectUser(&packagingUser, r.Form["inputUserId"][0], passwordmd5); err_s == nil {
					if len(packagingUser.Userid) == 0 {
						errmsg.Errcode = 1
						panic(errors.New("用户或密码错误！"))
					}
				} else {
					errmsg.Errcode = 1
					panic(errors.New("查询用户错误！"))
				}
				//
				products_type_num, err := strconv.Atoi(r.Form["productsType"][0])
				if err != nil {
					errmsg.Errcode = 1
					panic(errors.New("productsType不是有效的数字！"))
				}

				com_operation.GetNowPackingData().P_type = products_type_num
				com_operation.GetNowPackingData().P_name = r.Form["productsName"][0]
				com_operation.GetNowPackingData().Inputuser = r.Form["inputUserId"][0]
				com_operation.GetNowPackingData().Line_mark = packagingUser.Line_mark
				com_operation.GetNowPackingData().Line_mark_note = packagingUser.Line_mark_note

				now_packing_data := models.Now_packing_data{
					P_type:         products_type_num,
					P_name:         r.Form["productsName"][0],
					Inputuser:      r.Form["inputUserId"][0],
					Line_mark:      packagingUser.Line_mark,
					Line_mark_note: packagingUser.Line_mark_note,
				}
				if err_select := db_server_mysql.UpdateNow_packing_data(&now_packing_data); err_select != nil {
					errmsg.Errcode = 1
					panic(errors.New("更新正在生产信息错误！"))
				}

				view_body.GetHtmlBody().PackingProducts = r.Form["productsName"][0]
				view_body.GetHtmlBody().PackingInputUser = r.Form["inputUserId"][0]
				view_body.GetHtmlBody().Line_mark = packagingUser.Line_mark
				view_body.GetHtmlBody().Line_mark_note = packagingUser.Line_mark_note

				view_body.GetBodyCChan().DoShowBody <- 1 //通讯界面刷新

				//------写入正确值------
				errmsg.Errcode = 0
				if emsg, err := json.Marshal(errmsg); err == nil {
					w.Write([]byte(string(emsg)))
				}

			} else {
				errmsg.Errcode = 1
				panic(errors.New("缺少必要参数！"))
			}
		} //---

		if r.Form["dofunc"][0] == "getNowPackagingStatus" {
			//util.File_write_string("log_file", "errLog-"+util.GetNowTime("yyyy-MM"), "--"+RemoteIp(r))
			var now_packaging_status models.Now_packaging_status
			if err := db_server_mysql.SelectNow_packaging_status(&now_packaging_status); err == nil {
				if np_status, err2 := json.Marshal(now_packaging_status); err2 == nil {
					w.Write([]byte(string(np_status)))
				}
			} else {
				errmsg.Errcode = 1
				panic(errors.New("查询当前包装状态错误！"))
			}
		} //---

		if r.Form["dofunc"][0] == "getScanPrint" {
			var printdata models.PrintData
			if err_pd := db_server_mysql.SelectPrintData(&printdata); err_pd == nil {
				if p_tdata_str, err3 := json.Marshal(printdata); err3 == nil {
					w.Write([]byte(string(p_tdata_str)))
				} else {
					errmsg.Errcode = 1
					panic(errors.New("解析打印数据错误！"))
				}
			} else {
				errmsg.Errcode = 1
				panic(errors.New("取得打印数据错误！"))
			}
		} //---
		if r.Form["dofunc"][0] == "doCleanPackingInfo" {
			if len(r.Form["packid_post"]) > 0 {

				now_p_status := models.Now_packaging_status{
					Packid:      0,
					Packnum:     0,
					Fbillno:     "0",
					Fname:       "0",
					Fnumber:     "0",
					Sum_packnum: 0,
				}
				P_ID, _ := strconv.Atoi(r.Form["packid_post"][0])
				if err_pd := db_server_mysql.CleanNowPackagingStatus_D(&now_p_status, P_ID); err_pd == nil {
					view_body.GetHtmlBody().FBillNo = `--`
					view_body.GetHtmlBody().FNumber = `--`
					view_body.GetHtmlBody().PackingNumHTML = `--`
					view_body.GetHtmlBody().NowPackingNumHTML = `--`
					view_body.GetHtmlBody().PackingListHTML = ``
					view_body.GetBodyCChan().DoShowBody <- 1

					errmsg.Errcode = 0
					if emsg, err := json.Marshal(errmsg); err == nil {
						w.Write([]byte(string(emsg)))
					}
				} else {
					errmsg.Errcode = 1
					panic(errors.New("清除包装数据错误！"))
				}
			} else {
				errmsg.Errcode = 1
				panic(errors.New("缺少必要参数！"))
			}

		} //---
	}
}
